package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import utilities.Constants;
import utilities.Utils;
import controller.AddCandidateController;

public class AddCandidateView extends JFrame implements GenericView{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4271172775488712401L;
	private JPanel contentPane;
	private JTextField txtNombre;
	private static AddCandidateView instance = null;
	private AddCandidateController controller;
	private JLabel lblImagen;
	private String path = Constants.newUserPath;
	private String name = Constants.newUserName;
	
	/**
	 * Create the frame.
	 */
	public AddCandidateView() {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowListener() {
			@Override
			public void windowActivated(WindowEvent arg0) {
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				controller.handleButtonCancelar();
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
			}
        });
		setBounds(Constants.SRC_X, Constants.SRC_Y, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);
		setResizable(Boolean.FALSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblAgregarCandidato = new JLabel("Agregar Candidato");
		lblAgregarCandidato.setBounds(273, 29, 184, 15);
		contentPane.add(lblAgregarCandidato);
		
		txtNombre = new JTextField();
		getTxtNombre().setText("Nombre");
		getTxtNombre().setBounds(55, 173, 142, 44);
		contentPane.add(getTxtNombre());
		getTxtNombre().setColumns(20);
		
		JButton btnAgregarFoto = new JButton("Agregar Foto");
		btnAgregarFoto.setBounds(386, 343, 157, 25);
		btnAgregarFoto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonAddPicture();
			}
		});
		contentPane.add(btnAgregarFoto);
		
		JButton btnGuardar = new JButton("Guardar");
		btnGuardar.setBounds(370, 443, 117, 34);
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonGuardar();
			}
		});
		contentPane.add(btnGuardar);
		
		setLblImagen(new JLabel(""));
		getLblImagen().setBounds(370, 111, 267, 220);
		Icon icon = Utils.setImageToLabel(Constants.newUserPath, Constants.WIDTH, Constants.HEIGHT);
		getLblImagen().setIcon(icon);
		contentPane.add(getLblImagen());
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds(158, 443, 117, 34);
		contentPane.add(btnCancelar);
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonCancelar();
			}
		});
	}

	@Override
	public void showWindow() {
		this.setVisible(true);
	}

	@Override
	public void hideWindow() {
		this.setVisible(false);
	}
	
	public static AddCandidateView getInstance() {
		if (instance == null){
			instance = new AddCandidateView();
		}
		return instance;
	}

	public void setController(AddCandidateController controller) {
		this.controller = controller;
	}

	public void changeImage(File file) {
		this.path = file.getAbsolutePath();
		this.name = file.getName();
		this.getLblImagen().setIcon(Utils.setImageToLabel(file.getAbsolutePath(), Constants.WIDTH, Constants.HEIGHT));
	}

	public JTextField getTxtNombre() {
		return txtNombre;
	}

	public void setTxtNombre(JTextField txtNombre) {
		this.txtNombre = txtNombre;
	}

	public JLabel getLblImagen() {
		return lblImagen;
	}

	public void setLblImagen(JLabel lblImagen) {
		this.lblImagen = lblImagen;
	}
	
	public String getPath(){
		return this.path;
	}
	
	public void setPath(String path){
		this.path = path;
	}

	public String getName(){
		return this.name;
	}
}
